package com.pennphoto.client;

import java.util.Date;

import com.pennphoto.shared.ProfFieldVerifier;
import com.pennphoto.shared.StuFieldVerifier;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;


import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.i18n.client.HasDirection.Direction;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.datepicker.client.DatePicker;
import com.google.gwt.user.client.ui.ListBox;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Professor implements EntryPoint {
	

	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	public int flag=0;
	public int userid=-1;
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final ProfessorServiceAsync professorService = GWT
			.create(ProfessorService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		//AbsolutePanel rootPanel = new AbsolutePanel();
		final RootPanel rootPanel = RootPanel.get();
		//final Button b1 = new Button("Submit Info");
		final Label errorLabel = new Label();
		//final Label errorLabel = new Label();
		//rootPanel.addStyleName("Hi");
		//final Button b1 = new Button("Register");
		
		//this.add(rootPanel);
		rootPanel.setSize("848px", "511px");
		
		HTML html = new HTML("", true);
		rootPanel.add(html);
		
		HTML htmlfirstname = new HTML("<h3>Research Area:</h3", true);
		rootPanel.add(htmlfirstname, 226, 54);
		htmlfirstname.setSize("147px", "51px");
		
		final TextArea textArea = new TextArea();
		rootPanel.add(textArea, 379, 68);
		textArea.setSize("137px", "12px");
		
		
		HTML htmlwelcomeToAlan = new HTML("<h1 align=\"center\"><b><u>Welcome to Professor's Information Retriever</u></b></h1>", false);
		rootPanel.add(htmlwelcomeToAlan, 245, -17);
		htmlwelcomeToAlan.setSize("530px", "30px");
		final Button btnRegister = new Button("Submit Info");
		rootPanel.add(btnRegister, 420, 278);
		
		HTML html_1 = new HTML("", true);
		rootPanel.add(html_1, 341, 253);
		
		HTML htmllastname = new HTML("<h3>Title:</h3", true);
		rootPanel.add(htmllastname, 283, 83);
		htmllastname.setSize("75px", "41px");
		
		final TextArea textArea_1 = new TextArea();
		rootPanel.add(textArea_1, 379, 96);
		textArea_1.setSize("137px", "18px");
		
		HTML htmldateOfBirthmmddyyyy = new HTML("<h3>Room Number:</h3", true);
		rootPanel.add(htmldateOfBirthmmddyyyy, 245, 127);
		htmldateOfBirthmmddyyyy.setSize("260px", "40px");
		
		Label label = new Label("");
		rootPanel.add(label, 484, 338);
		
		HTML html_2 = new HTML("", true);
		rootPanel.add(html_2, 339, 397);
		//rootPanel.setSize("1000px", "700px");
		rootPanel.add(errorLabel);
		errorLabel.setSize("860px", "20px");
		
		final TextArea textArea_2 = new TextArea();
		rootPanel.add(textArea_2, 379, 141);
		textArea_2.setSize("137px", "20px");
		
		

		// Create the popup dialog box
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("Remote Procedure Call");
		dialogBox.setAnimationEnabled(true);
		final Button closeButton = new Button("Close");
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		final Label textToServerLabel = new Label();
		final HTML serverResponseLabel = new HTML();
		VerticalPanel dialogrootPanelanel = new VerticalPanel();
		dialogrootPanelanel.addStyleName("dialogrootPanelanel");
		dialogrootPanelanel.add(new HTML("<b>Sending name to the server:</b>"));
		dialogrootPanelanel.add(textToServerLabel);
		dialogrootPanelanel.add(new HTML("<br><b>Server replies:</b>"));
		dialogrootPanelanel.add(serverResponseLabel);
		dialogrootPanelanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogrootPanelanel.add(closeButton);
		dialogBox.setWidget(dialogrootPanelanel);

		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				btnRegister.setEnabled(true);
				btnRegister.setFocus(true);
			}
		});

		// Create a handler for the loginButton and nameField
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the RegisterButton.
			 */
			public void onClick(ClickEvent event) {
				try {
					sendNameToServer();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					dialogBox
					.setText("Remote Procedure Call - Failure");
			serverResponseLabel
					.addStyleName("serverResponseLabelError");
			serverResponseLabel.setHTML(SERVER_ERROR);
			dialogBox.center();
			closeButton.setFocus(true);
				}
				if(flag==1){
				RootPanel.get().clear();
				//Anand's page
				if (userid!=-1){
					rootPanel.get().clear();
					UserProfile user =new UserProfile(userid);
					user.onModuleLoad();
					
				}
				}
				//RootPanel.get().add(new Pan(nameField.getText()));
				
			}

			/**
			 * Fired when the user types in the Fields.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					try {
						sendNameToServer();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						dialogBox
						.setText("Remote Procedure Call - Failure");
				serverResponseLabel
						.addStyleName("serverResponseLabelError");
				serverResponseLabel.setHTML(SERVER_ERROR);
				dialogBox.center();
				closeButton.setFocus(true);
					}
					if(flag==1){
						RootPanel.get().clear();
						if (userid!=-1){
							rootPanel.get().clear();
							UserProfile user =new UserProfile(userid);
							user.onModuleLoad();
							
						}
						}
					
					//RootPanel.get().add(new Pan(nameField.getText()));
					//sendNameToServer();
				}
			}
			
			private void sendNameToServer() throws Exception {
				//System.out.println("Hello");
				// First, we validate the input.
				errorLabel.setText("");
				String textToServer = textArea.getText();
				String text1ToServer =textArea_1.getText();
				String text2ToServer =textArea_2.getText();
				if (ProfFieldVerifier.isValidName(textToServer,text1ToServer,text2ToServer)!=null) {
					String str=ProfFieldVerifier.isValidName(textToServer,text1ToServer,text2ToServer);
					//errorLabel.setText("Please enter at least four characters");
					errorLabel.setText(str);
					if(str.equals("")){
						flag=1;
					}
					return;
				}
				// Then, we send the input to the server.
				btnRegister.setEnabled(false);
				textToServerLabel.setText(textToServer);
				textToServerLabel.setText(text1ToServer);
				textToServerLabel.setText(text2ToServer);
				
				serverResponseLabel.setText("");
	
				professorService.greetServer3(textToServer,text1ToServer,text2ToServer,
						new AsyncCallback<Integer>() {
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								dialogBox
										.setText("Remote Procedure Call - Failure");
								serverResponseLabel
										.addStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(SERVER_ERROR);
								dialogBox.center();
								closeButton.setFocus(true);
							}

							public void onSuccess(Integer result) {
								userid=result;
								dialogBox.setText("Remote Procedure Call");
								serverResponseLabel
										.removeStyleName("serverResponseLabelError");
								//serverResponseLabel.setHTML(result);
								if (result!=-1){
									rootPanel.get().clear();
									UserProfile user =new UserProfile(result);
									user.onModuleLoad();
									
								}
								//dialogBox.center();
								//closeButton.setFocus(true);
							}
						});
			}
		}
		
		
		
		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		btnRegister.addClickHandler(handler);
		textArea.addKeyUpHandler(handler);
		textArea_1.addKeyUpHandler(handler);
		textArea_2.addKeyUpHandler(handler);
		
		
				
	}
}

